/**
 * 变电站信息管理
 */
Ext.define('Module.jc.JcBdz', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.jcBdz',
	layout: 'border',
	config:{
		ssdwId:''
	},
	initComponent : function() {
		var me = this;
		var store=Ext.create('Ext.data.Store',{
			autoLoad: false,
			fields: ['id','bdzCode','bdzName','upuser','uptime','name','jcDsdwId','dsdwName'],
	        proxy: {
	            type: 'ajax',
	            url: 'jc/JcBdzAction/getJcBdz.do'
	        }
		});
		
		var jcDsdwStore=Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields: ['id','code','dsdwName'],
	        proxy: {
	            type: 'ajax',
	            url: 'jc/JcBdzAction/getJcDsdw.do'
	        }
		});
		
		
		
		
		var dsdwPanel = Ext.create('Ext.grid.Panel',{
			title: '地市单位',
			region: 'west',
			padding: '0 1 0 0',
			width:280,
			store: jcDsdwStore,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: '100%',
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [{
						xtype: 'toolbar',
						items: [{
							xtype:"textfield",
							fieldLabel : '地市单位',	
							maxLength:40,
							labelWidth:70,
							width:190
						},{
							text:"查询",
							iconCls: 'icon-search',
							handler: function(){
								var tbar = this.up('toolbar');
								var items = tbar.items.items;
								dsdwPanel.store.proxy.extraParams = {
									dsdwName: items[0].getSubmitValue()
								};
								dsdwPanel.store.load();
							}
						}
					]
				 }]
			}],
			columns: [{
	            flex: 2,
	            dataIndex: 'dsdwName'
			}],
			listeners:{
				itemclick:function(tree, record, item, index, e, eOpts) { 
					me.setSsdwId(record.data.id);
					store.proxy.extraParams={
							jcDsdwId:record.data.id
					};
					store.loadPage(1);
				}
			}
			
		});
		
		var jcBdzPanel = me.jcBdzPanel= Ext.create('Ext.grid.Panel',{
			title: '变电站信息列表',
			region: 'center',
			padding: '0 1 0 0',
			store: store,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: '100%',
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [{
						xtype: 'toolbar',
						items: [{
							text: '添加信息',
							iconCls: 'icon-add',
							handler: function(){
								var rec = dsdwPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.addJcBdz(rec);
									Ext.getCmp(me.id+"jcDsdwId").setValue(rec.data.id);
								}
								else{
									Ext.Msg.alert('提示', '请选择要添加的地市单位数据！');
								}
							}
						},{
							text: '修改信息',
							iconCls: 'icon-edit',
							handler: function(){
								var rec = jcBdzPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.updateJcBdz(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要修改的信息！');
								}
							}
						},{
							text: '删除信息',
							iconCls: 'icon-delete',
							handler: function(){
								var rec = jcBdzPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.delJcBdz(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要删除的信息！');
								}
							}
						},{
							xtype:"textfield",
							fieldLabel : '变电站名称',	
							maxLength:40,
							labelWidth:70,
							width:190
						},{
							xtype:"textfield",
							fieldLabel : '变电站编码',	
							maxLength:20,
							labelWidth:70,
							width:190
						},{
							text:"查询",
							iconCls: 'icon-search',
							handler: function(){
								var tbar = this.up('toolbar');
								var items = tbar.items.items;
								if(me.getSsdwId() !=''){
									jcBdzPanel.store.proxy.extraParams = {
											bdzName: items[3].getSubmitValue(),
											bdzCode: items[4].getSubmitValue(),
											jcDsdwId:me.getSsdwId()
									};
									jcBdzPanel.store.loadPage(1);
								}
							}
						}
					]
					 }]
			}],
			columns: [{
	            text: '变电站名称',
	            flex: 2,
	            dataIndex: 'bdzName'
			},{
	            text: '变电站编码',
	            flex: 1,
	            dataIndex: 'bdzCode'
			},{
	            text: '更新用户',
	            flex: 2,
	            dataIndex: 'name'
			},{
	            text: '更新时间',
	            flex: 2,
	            dataIndex: 'uptime',
	            renderer: formatDate
			}]
		});
		
		
		me.items = [dsdwPanel,jcBdzPanel];
		
		me.callParent();
	},
	updateJcBdz: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 410,
			height: 180,
			iconCls: 'icon-edit',
			modal: true,
			layout: 'fit',
			title: '修改信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'jc/JcBdzAction/updateJcBdz.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '修改成功！');
            	    	    		formp.up('window').close();
            	    	    		me.jcBdzPanel.store.load({
            	    	    			params:{
            	    	    				jcDsdwId:rec.data.jcDsdwId
            	    	    			}
            	    	    			
            	    	    		});
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '修改失败！详情：'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	addJcBdz: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 410,
			height: 180,
			iconCls: 'icon-add',
			modal: true,
			layout: 'fit',
			title: '添加信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'jc/JcBdzAction/addJcBdz.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		formp.up('window').close();
            	    	    		me.jcBdzPanel.store.load({
            	    	    			params:{
            	    	    				jcDsdwId:rec.data.id
            	    	    			}
            	    	    		});
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '保存失败！详情：'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	delJcBdz: function(rec){
		var me = this;
		if(rec){
			Ext.Msg.confirm('提示', '你确定要删除当前信息吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'jc/JcBdzAction/deleteJcBdz.do',
					    params: rec.data,
					    success: function(response){
        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	if(rs.rst){
        	    	    		Ext.Msg.alert('提示', '删除成功！');
        	    	    		me.jcBdzPanel.store.load({
        	    	    			params:{
        	    	    				jcDsdwId:rec.data.jcDsdwId
        	    	    			}
        	    	    		});
        	    	    	}
        	    	    	else{
        	    	    		Ext.Msg.alert('提示', '删除失败！详情：<br>'+rs.msg);
        	    	    	}
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的信息！');
		}
	},
	getForm: function(config,readOnly){
		var me= this;
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			defaults: {anchor: '100%'},
			items: [{
					name: 'id',
					xtype: 'hidden'
				},{
					name: 'jcDsdwId',
					xtype: 'hidden',
					id:me.id+"jcDsdwId"
				},{
					fieldLabel: '地市单位名称',
					name: 'dsdwName',
					id:me.id+"dsdwName",
					readOnly:true,
					maxLength:20,
					xtype: 'textfield'
				},{
					fieldLabel: '变电站名称',
					name: 'bdzName',
					xtype: 'textfield',
					maxLength:20,
					allowBlank: false
				},{
					fieldLabel: '变电站编码',
					name: 'bdzCode',
					maxLength:50,
					xtype: 'textfield',
					allowBlank: false
			}]
		},config);
	}
});